package ten;

import java.util.ArrayList;
import java.util.List;

public class HappyCinema1 {
    public static void main(String[] args) {
        List<Integer> all_list = new ArrayList<Integer>(){{
            for (int i = 1; i < 6; i++) {
                add(i);
            }
        }};
        List<Integer> need_list1 = new ArrayList<Integer>(){{
            add(2);
            add(5);
        }};
        List<Integer> need_list2 = new ArrayList<Integer>(){{
            add(1);
            add(5);
        }};

        Cinema1 c = new Cinema1(all_list, "快乐影院");
        new Thread(new Customer1(c, need_list1), "老裴").start();
        new Thread(new Customer1(c, need_list2), "老高").start();
    }
}

class Cinema1 {
    List<Integer> available;
    String name;

    public Cinema1(List<Integer> available, String name) {
        this.available = available;
        this.name = name;
    }

    public boolean buyTickets(List<Integer> seats) {
        System.out.println("可用位置为: " + available);
        List<Integer> copy = new ArrayList<>();
        copy.addAll(available);
        copy.removeAll(seats);
        if(available.size() - copy.size() != seats.size()){
            return false;
        }
        available = copy;
        return true;
    }
}

class Customer1 implements Runnable {
    Cinema1 cinema;
    List<Integer> seats;

    public Customer1(Cinema1 cinema, List<Integer> seats) {
        this.cinema = cinema;
        this.seats = seats;
    }

    @Override
    public void run() {
        synchronized (cinema) {
            boolean flag = cinema.buyTickets(seats);
            if (flag) {
                System.out.println("出票成功,"
                        + Thread.currentThread().getName()
                        + "-<位置为: "
                        + seats);
            } else {
                System.out.println("出票失败,"
                        + Thread.currentThread().getName()
                        + "-<位置不够");
            }
        }
    }
}
